package de.g18.llmaa.gui.workspace.internalFrame.xmlEinlesen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import de.g18.llmaa.gui.App_LLMAA_Controler;
import de.g18.llmaa.gui.App_LLMAA_Model;
import de.g18.llmaa.gui.IApp_Model;
import de.g18.llmaa.gui.workspace.internalFrame.Abstract_InternalFrame;
import de.g18.llmaa.logic.xmlRead.XML_LizenzRead;

public class InternalFrame_XmlEinlesen extends Abstract_InternalFrame {

	private JFileChooser xmlFileChooser;


	public InternalFrame_XmlEinlesen() {
		
		this.setContentPane(getXmlFileChooser());
		setSize(500, 400);
		setTitle("Xml Datei Datei Einlesen");

	}

	public JFileChooser getXmlFileChooser() {
		if (xmlFileChooser == null) {
			xmlFileChooser = new JFileChooser() {
				public void cancelSelection() {
					
					closeFrame();
				}
			};
			xmlFileChooser.setFileFilter(new FileFilter() {
				@Override
				public boolean accept(File f) {
					return f.isDirectory()
							|| f.getName().toLowerCase().endsWith(".xml");
				}

				@Override
				public String getDescription() {
					return "Xml";
				}

			});

			xmlFileChooser.addActionListener(new ActionListener() {

				public void actionPerformed(ActionEvent e) {

					try {
						XML_LizenzRead read = new XML_LizenzRead(xmlFileChooser
								.getSelectedFile());
						
						App_LLMAA_Model model = new App_LLMAA_Model();
						model.setList_ProductKey(read.getList_ProductKey());
						App_LLMAA_Controler.getInstanze().setModel(model);

					} catch (NullPointerException error) {
						showMessageXMLFehlerHaft();
					}

				}

			}

			);
		}
		return xmlFileChooser;
	}

	private void showMessageXMLFehlerHaft() {
		JOptionPane.showMessageDialog(this,
				"Es Wurden keine Lizenzen, \n in diese Datei gefunden");

	}

	void closeFrame() {
		try {
			setClosed(true);
		} catch (PropertyVetoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
